package com.rk.kbk.controller;


import com.rk.kbk.common.FailException;
import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.EachPictureDao;
import com.rk.kbk.model.EachPicture;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**广告轮播图controller
 * Created by bin on 2017/2/8.
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/eachPicture")
public class EachPictureController{
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private EachPictureDao eachPictureDao;

    /**
     * 删除轮播图
     */
    @RequestMapping(value = "/deleteEachPicture",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp delete(Long id) {
        log.debug("删除轮播图");
        eachPictureDao.delete(id);
        return JsonResp.ok();

    }

    /**
     * 添加/修改轮播图
     */
    @RequestMapping(value = "/addEachPicture",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addEachPicture(@RequestBody EachPicture eachPicture){
        log.debug("添加/修改轮播图");

        if(eachPicture.getId() != null && eachPicture.getId()!=0) {
            EachPicture ep = eachPictureDao.findOne(eachPicture.getId());
            if (eachPicture.getAttachmentUrl().equals("")) {
                eachPicture.setAttachmentUrl(null);
            }
            Long sTime = eachPicture.getStartTime().getTime();
            Long eTime = eachPicture.getEndTime().getTime();
            Long sysTime = new Date().getTime();
            if(eachPicture.getDestatus()==1){
                if(sysTime > sTime && sysTime < eTime){
                    eachPicture.setDestatus(1);
                }else{
                    eachPicture.setDestatus(2);
                }
            }else{
                if(sysTime > sTime && sysTime < eTime){
                    eachPicture.setDestatus(0);
                }else {
                    eachPicture.setDestatus(2);
                }
            }
            ep.setTitle(eachPicture.getTitle());
            ep.setType(eachPicture.getType());
            ep.setAdminId(eachPicture.getAdminId());
            ep.setAttachmentUrl(eachPicture.getAttachmentUrl());
            ep.setLinkUrl(eachPicture.getLinkUrl());
            ep.setContent(eachPicture.getContent());
            ep.setId(eachPicture.getId());
            ep.setDestatus(eachPicture.getDestatus());
            ep.setAreaCode(eachPicture.getAreaCode());
            ep.setStartTime(eachPicture.getStartTime());
            ep.setEndTime(eachPicture.getEndTime());
            ep.setAreaCode(eachPicture.getAreaCode());
            ep.setProvince(eachPicture.getProvince());
            ep.setCity(eachPicture.getCity());
            ep.setArea(eachPicture.getArea());
            ep.setAdvertType(eachPicture.getAdvertType());
            eachPictureDao.save(ep);
        }
        if (null == eachPicture.getType() || "".equals(eachPicture.getType()+"")) {
            throw new FailException("所属模块类型不能为空");
        }
        eachPictureDao.save(eachPicture);
        return JsonResp.ok(eachPicture);
    }

    /**
     * 获取轮播图详情
     */
    @RequestMapping(value = "/details",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp details(Long id){
        log.debug("获取轮播图详情");
            EachPicture eachPicture = eachPictureDao.findOne(id);
            return JsonResp.ok(eachPicture);
    }

    /**
     * 获取轮播图列表
     */
    @RequestMapping(value = "/getList",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp getList(Integer type, Integer destatus){
        log.debug("获取轮播图列表");
        List<EachPicture> eachPictures  ;
        if (type==-1) {
            if(destatus==-1){
                eachPictures=eachPictureDao.findAll();
            }else{
                eachPictures=eachPictureDao.findByDestatusOrderByUptDatetimeAsc(destatus);
            }
        }else{
            if(destatus==-1){
                eachPictures=eachPictureDao.findByTypeOrderByUptDatetimeAsc(type);
            }else {
                eachPictures=eachPictureDao.findByTypeAndDestatusOrderByUptDatetimeAsc(type,destatus);
            }
        }
        return JsonResp.ok(eachPictures);
    }

    /**
     * 发布广告
     */
    @RequestMapping(value = "/release",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp release(Long id){
        log.debug("发布广告");
        EachPicture eachPicture = eachPictureDao.findOne(id);
        eachPicture.setDestatus(1);
        eachPictureDao.save(eachPicture);
        return JsonResp.ok();
    }

    /**
     * @获取特定已发布广告
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/allEachPicture",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findAllEachPicture(Integer type,Integer advertType) {
        log.debug("获取特定已发布广告");
        return JsonResp.ok(eachPictureDao.findByDestatusAndTypeAndAdvertType(1,type,advertType));
    }
/*    @RequestMapping(value = "/deleteEach",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteEach() {
        log.debug("处理已过期轮播图");
        List<EachPicture> eachPictures = eachPictureDao.findAll();
        Long sysTime = new Date().getTime();
        for (EachPicture eachPicture : eachPictures) {
            Long uTime = eachPicture.getValidity().getTime();
            if (sysTime > uTime) {
                eachPicture.setDestatus(1);
            } else {
                eachPicture.setDestatus(0);
            }
            EachPicture ep = eachPictureDao.findOne(eachPicture.getId());
            ep.setTitle(eachPicture.getTitle());
            ep.setType(eachPicture.getType());
            ep.setAdminId(eachPicture.getAdminId());
            ep.setAttachmentUrl(eachPicture.getAttachmentUrl());
            ep.setLinkUrl(eachPicture.getLinkUrl());
            ep.setContent(eachPicture.getContent());
            ep.setId(eachPicture.getId());
            ep.setDestatus(eachPicture.getDestatus());
            ep.setValidity(eachPicture.getValidity());
            eachPictureDao.save(ep);
        }
        return JsonResp.ok();
    }*/
   /* @RequestMapping
    @RequestDescription("添加/修改轮播图")
    @Transactional(rollbackFor = Throwable.class)
    public JsonResp<?> add(@RequestBody EachPicture eachPicture) {
        if(eachPicture.getId() != null && eachPicture.getId()!=0) {
            if (eachPicture.getAttachmentUrl().equals("")) {
                eachPicture.setAttachmentUrl(null);
            }
            Long uTime = eachPicture.getValidity().getTime();
            Long sysTime = new Date().getTime();
            if (sysTime > uTime) {
                eachPicture.setDestatus(1);
            }else{
                eachPicture.setDestatus(0);
            }
            eachPicture.setGmtDatetime(eachPictureService.selectByPrimaryKey(eachPicture.getId()).getGmtDatetime());
            int i = eachPictureService.updateByPrimaryKeySelective(eachPicture);
            if (i > 0) {
                return JsonResp.ok();
            }else{
                throw new FailException();
            }
        }
        if (null == eachPicture.getType() || "".equals(eachPicture.getType()+"")) {
            throw new FailException("所属模块类型不能为空");
        }
        eachPicture.setDestatus(0);
        return eachPictureService.insertJson(eachPicture);
    }

    @RequestMapping
    @RequestDescription("删除轮播图")
    @Transactional(rollbackFor = Throwable.class)
    public JsonResp<?> delete(Long id) {
        boolean b = eachPictureService.deleteIds(new Long[]{id});
        if (b) {
            return JsonResp.ok();
        }else {
            throw new FailException();
        }
    }

    @RequestMapping
    @RequestDescription("获取轮播图详情")
    @Transactional(rollbackFor = Throwable.class)
    public JsonResp<?> details(Long id) {
        EachPicture eachPicture = eachPictureService.selectByPrimaryKey(id);
        return JsonResp.ok(eachPicture);
    }

    @RequestMapping
    @RequestDescription("获取轮播图列表")
    @Transactional(rollbackFor = Throwable.class)
    public JsonResp<?> getList(Integer type, Integer destatus, Boolean isAdmin) {
        if (null==type) {
            throw new EmptyException();
        }

        Example example = eachPictureService.getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", type);

        if (isAdmin != null) {
            criteria.andEqualTo("destatus",destatus);
        }else{
            criteria.andEqualTo("destatus",0);
        }

        example.orderBy("uptDatetime").desc();

        List<EachPicture> eachPictures = eachPictureService.selectByExample(example);

        return JsonResp.list(eachPictures);
    }

    @RequestDescription("处理已过期轮播图")
    public JsonResp<?> deleteEach() {
        List<EachPicture> eachPictures = eachPictureService.selectAll();
        Long sysTime = new Date().getTime();
        for (EachPicture eachPicture : eachPictures) {
            Long uTime = eachPicture.getValidity().getTime();
            if (sysTime > uTime) {
                eachPicture.setDestatus(1);
            }else{
                eachPicture.setDestatus(0);
            }
            int i = eachPictureService.updateByPrimaryKeySelective(eachPicture);
            if (i <= 0) {
                throw new FailException();
            }
        }
        return JsonResp.ok();
    }*/

}
